package others;

import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import util.Database;

public class IPtransfomer {

	public Database db;
	
	
	public IPtransfomer(){
		db = new Database("com.mysql.jdbc.Driver",
				"jdbc:mysql://127.0.0.1:3306/antsoldier", "admin", "123456");
	}
	
	
	
	public static void main(String[] args) {
		String file = "worm.txt";
		String writerfile = "out.txt";
		String[] StrArray = new String[4];
		ArrayList<String> ipList = new ArrayList<String>();
		
		try{                                                                                                        
			FileReader fr = new FileReader(file);                                                                       
			BufferedReader br = new BufferedReader(fr);                                                                 
			                                                                                                            
			FileWriter fw = new FileWriter(writerfile); //写文件操作，把得到的file对应的文件中内容写入，writerfile中去。
			BufferedWriter bw = new BufferedWriter(fw);                                                                 
			String readoneline;     
			
			Pattern p = Pattern.compile("[^\\s]*\\s*([^\\s]*+)\\s*([^\\s]*)\\s*([^\\s]*)\\s*([^\\s]*)\\s*([^\\s]*)");
			Matcher m;
			int l =0;
			while((l = br.read()) != -1){                                                                               
			    readoneline = br.readLine();
			   /* StrArray = readoneline.split(" "); 
			    for(int i=0;i<4;i++)
			    	System.out.println(StrArray[i]);*/
			    
			    m = p.matcher(readoneline);
			    if (m.find()){
			    	System.out.println("------" + m.group(1));
			    	System.out.println("-------" + m.group(2));
			    	System.out.println("-------" + m.group(3));
			    	System.out.println("-------" + m.group(4));
			    }
			    Iterator<String> it=ipList.iterator();
			    
			    boolean exist1 = false;
			    boolean exist2 = false;
			    while(it.hasNext()){
			    	if(it.next().equals(m.group(3)))
			    		exist1 = true; 
			    }
			    it=ipList.iterator();
			    while(it.hasNext()){
			    	if(it.next().equals(m.group(4)))
			    		exist2 = true; 
			    }
			    
			    if(exist1 == false){
			    ipList.add(m.group(3));
			    bw.write(m.group(3));                                                                                  
			    bw.newLine();
			    }
			    if(exist2 == false){
			    	ipList.add(m.group(4));			    	
				    bw.write(m.group(4));                                                                                  
				    bw.newLine(); 		    	
			    }
			    			    	
			   // bw.write(readoneline);                                                                                  
			   // bw.newLine();                                                                                           
			    System.out.println("++++++" + readoneline); 			    
			    
			}                                                                                                           
			bw.flush();                                                                                                 
			br.close();                                                                                                 
			bw.close();                                                                                                 
			br.close();                                                                                                 
			fw.close();                                                                                                 
			fr.close();                                                                                                 
			}catch(IOException e){                                                                                      
			    e.printStackTrace();                                                                                    
			}      
		
	}
	
	
    /**
     * 加载数据到内存中
     */
    public void loaddata(){
    	
    	ResultSet rs = db.database_execute_single_command("SELECT COUNT(*) FROM event");
		int count = 0;
		try {
			while (rs.next()) {
				count = rs.getInt("count(*)");
			}
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Count is " + count);
		
		
		db.database_execute_no_query("");
    }
}
